package br.com.aquinoevasconcelos.servlets.administrador;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.aquinoevasconcelos.excecao.ObjNaoEncontradoException;
import br.com.aquinoevasconcelos.fachada.Fachada;
import br.com.aquinoevasconcelos.user.UsuarioAdm;

/**
 * Servlet implementation class AutenticarAdministrador
 */
@WebServlet("/AutenticarAdministrador")
public class AutenticarAdministrador extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
    public AutenticarAdministrador() {
        super();

    }
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	Fachada fachada = Fachada.getInstancia();
    	
    	String login = request.getParameter("login");
    	String senha = request.getParameter("senha");
    	
    	
    	try {
    	
			UsuarioAdm usu = fachada.AutenticacaoADM(login, senha);
    		
    		HttpSession session = request.getSession();
    		session.setAttribute("sessaoAdministrador",usu);
    		
    		//System.out.println(usu.getNome());
    		
    		//request.setAttribute("administrador", usu);
    		
			RequestDispatcher rd = request.getRequestDispatcher("EncaminharPainelAdm");
			rd.forward(request, response);
    	//response.sendRedirect("admin/inicio_painel.jsp");
    		
		} catch (ObjNaoEncontradoException e) {
			
			request.getRequestDispatcher("login_admin.jsp?msg=erro").forward(request, response);
		}
    	
    
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		service(request, response);

	}

}
